package com.quxiang.pay.service;

import java.net.URLEncoder;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.quxiang.pay.model.AccessTokenModel;
import com.quxiang.pay.model.WxUserInfo;
import com.quxiang.pay.util.ConfigUtils;
import com.quxiang.pay.util.HttpClientUtils;

@Service
public class WechatServiceImpl implements WechatService {

	private final static Logger logger = LoggerFactory.getLogger(WechatServiceImpl.class);
	private final static ObjectMapper mapper = new ObjectMapper();

	@Override
	public AccessTokenModel accessToken(String code) {

		StringBuffer buffer = new StringBuffer("https://api.weixin.qq.com/sns/oauth2/access_token");
		buffer.append("?appid=");
		buffer.append(ConfigUtils.wx_appid);
		buffer.append("&secret=");
		buffer.append(ConfigUtils.wx_appsecret);
		buffer.append("&code=");
		buffer.append(code);
		buffer.append("&grant_type=authorization_code");
		logger.info("wechat 获取 accessToken url = {}", buffer.toString());
		String result = HttpClientUtils.httpGet(buffer.toString());
		if (StringUtils.isNotEmpty(result)) {
			logger.info("wechat 获取 accessToken result = {}", result);
			try {
				return mapper.readValue(result, AccessTokenModel.class);
			} catch (Exception e) {
				logger.error("wechat 获取 accessToken error = {}", e);
			}
		} else {
			logger.info("wechat 获取 accessToken result is null  {}", result);
		}
		return null;
	}

	@Override
	public WxUserInfo getUserInfo(String accessToken, String openid) {

		StringBuffer buffer = new StringBuffer("https://api.weixin.qq.com/sns/userinfo");
		buffer.append("?access_token=");
		buffer.append(accessToken);
		buffer.append("&openid=");
		buffer.append(openid);
		buffer.append("&lang=zh_CN");
		logger.info("wechat 获取 getUserInfo url = {}", buffer.toString());
		String result = HttpClientUtils.httpGet(buffer.toString());
		if (StringUtils.isNotEmpty(result)) {
			logger.info("wechat 获取 getUserInfo result = {}", result);
			try {
				return mapper.readValue(result, WxUserInfo.class);
			} catch (Exception e) {
				logger.error("wechat 获取 getUserInfo error = {}", e);
			}
		} else {
			logger.info("wechat 获取 getUserInfo result is null  {}", result);
		}
		return null;
	}

	@Override
	public String wxCymjAuth() {

		try {
			String wx_url = "https://open.weixin.qq.com/connect/oauth2/authorize" + "?appid="
					+ ConfigUtils.wx_appid + "&redirect_uri="
					+ URLEncoder.encode(ConfigUtils.wx_cymj_cburl, "utf-8") + "&response_type=code"
					+ "&scope=snsapi_userinfo&state=qx" + new Date().getTime() + "#wechat_redirect";
			logger.error("wxAuth url = {} ", wx_url);
			return wx_url;
		} catch (Exception e) {
			logger.error("wxAuth URLEncoder.encode error = {} ", e);
		}

		return null;

	}

	@Override
	public String wxXunLeAuth() {

		try {
			String wx_url = "https://open.weixin.qq.com/connect/oauth2/authorize" + "?appid="
					+ ConfigUtils.wx_appid + "&redirect_uri="
					+ URLEncoder.encode(ConfigUtils.wx_xunle_cburl, "utf-8") + "&response_type=code"
					+ "&scope=snsapi_userinfo&state=qx" + new Date().getTime() + "#wechat_redirect";
			logger.error("wxAuth url = {} ", wx_url);
			return wx_url;
		} catch (Exception e) {
			logger.error("wxAuth URLEncoder.encode error = {} ", e);
		}

		return null;

	}

	@Override
	public String wxMaqueAuth() {

		try {
			String wx_url = "https://open.weixin.qq.com/connect/oauth2/authorize" + "?appid="
					+ ConfigUtils.wx_appid + "&redirect_uri="
					+ URLEncoder.encode(ConfigUtils.wx_maque_cburl, "utf-8") + "&response_type=code"
					+ "&scope=snsapi_userinfo&state=qx" + new Date().getTime() + "#wechat_redirect";
			logger.error("wxAuth url = {} ", wx_url);
			return wx_url;
		} catch (Exception e) {
			logger.error("wxAuth URLEncoder.encode error = {} ", e);
		}

		return null;

	}

	@Override
	public String wxAuth(String cburl) {

		try {
			String wx_url = "https://open.weixin.qq.com/connect/oauth2/authorize" + "?appid="
					+ ConfigUtils.wx_appid + "&redirect_uri=" + URLEncoder.encode(cburl, "utf-8")
					+ "&response_type=code" + "&scope=snsapi_userinfo&state=qx" + new Date().getTime()
					+ "#wechat_redirect";
			logger.error("wxAuth url = {} ", wx_url);
			return wx_url;
		} catch (Exception e) {
			logger.error("wxAuth URLEncoder.encode error = {} ", e);
		}

		return null;

	}

}
